<template>
  <div class="file-preview-component">
    <!-- 文件列表 -->
    <div class="file-list" v-if="files.length > 0">
      <div class="list-header">
        <h4>
          <i class="fas fa-folder-open"></i>
          文件列表 ({{ files.length }})
        </h4>
        <div class="list-actions">
          <el-button 
            @click="toggleViewMode"
            type="text"
            size="small"
          >
            <i :class="viewMode === 'grid' ? 'fas fa-list' : 'fas fa-th'"></i>
            {{ viewMode === 'grid' ? '列表视图' : '网格视图' }}
          </el-button>
          <el-button 
            @click="downloadAll"
            type="primary"
            size="small"
            v-if="files.length > 1"
          >
            <i class="fas fa-download"></i>
            下载全部
          </el-button>
        </div>
      </div>
      
      <div class="file-grid" :class="{ 'list-mode': viewMode === 'list' }">
        <div 
          v-for="(file, index) in files" 
          :key="file.id || index"
          class="file-item"
          :class="{ active: selectedFileIndex === index }"
          @click="selectFile(index)"
        >
          <!-- 文件图标/缩略图 -->
          <div class="file-thumbnail">
            <img 
              v-if="file.type && file.type.startsWith('image/')"
              :src="file.url || file.preview"
              :alt="file.name"
              class="thumbnail-image"
              @error="handleImageError"
            />
            <div v-else class="file-icon">
              <i :class="getFileIcon(file)"></i>
            </div>
            
            <!-- 文件类型标签 -->
            <div class="file-type-badge">
              {{ getFileExtension(file.name) }}
            </div>
          </div>
          
          <!-- 文件信息 -->
          <div class="file-info">
            <div class="file-name" :title="file.name">{{ file.name }}</div>
            <div class="file-meta">
              <span class="file-size">{{ formatFileSize(file.size) }}</span>
              <span class="file-date" v-if="file.lastModified">
                {{ formatDate(file.lastModified) }}
              </span>
            </div>
          </div>
          
          <!-- 文件操作 -->
          <div class="file-actions">
            <el-button 
              @click.stop="previewFile(file, index)"
              type="primary"
              size="small"
              circle
              :disabled="!canPreview(file)"
            >
              <i class="fas fa-eye"></i>
            </el-button>
            <el-button 
              @click.stop="downloadFile(file)"
              type="success"
              size="small"
              circle
            >
              <i class="fas fa-download"></i>
            </el-button>
            <el-button 
              @click.stop="removeFile(index)"
              type="danger"
              size="small"
              circle
              v-if="allowRemove"
            >
              <i class="fas fa-trash"></i>
            </el-button>
          </div>
        </div>
      </div>
    </div>

    <!-- 空状态 -->
    <div class="empty-state" v-else>
      <div class="empty-icon">
        <i class="fas fa-folder-open"></i>
      </div>
      <div class="empty-text">
        <h3>暂无文件</h3>
        <p>还没有上传任何文件</p>
      </div>
    </div>

    <!-- 文件预览对话框 -->
    <el-dialog
      v-model="previewDialogVisible"
      :title="currentFile?.name || '文件预览'"
      width="90%"
      :before-close="closePreview"
      class="file-preview-dialog"
    >
      <div class="preview-container" v-if="currentFile">
        <!-- 预览内容 -->
        <div class="preview-content">
          <!-- 图片预览 -->
          <div v-if="isImageFile(currentFile)" class="image-preview">
            <div class="image-container">
              <img 
                :src="currentFile.url || currentFile.preview"
                :alt="currentFile.name"
                class="preview-image"
                @load="onImageLoad"
                @error="onImageError"
              />
              
              <!-- 图片工具栏 -->
              <div class="image-toolbar">
                <el-button-group>
                  <el-button @click="zoomIn" size="small">
                    <i class="fas fa-search-plus"></i>
                  </el-button>
                  <el-button @click="zoomOut" size="small">
                    <i class="fas fa-search-minus"></i>
                  </el-button>
                  <el-button @click="resetZoom" size="small">
                    <i class="fas fa-expand-arrows-alt"></i>
                  </el-button>
                  <el-button @click="rotateImage" size="small">
                    <i class="fas fa-redo"></i>
                  </el-button>
                </el-button-group>
              </div>
            </div>
          </div>
          
          <!-- PDF 预览 -->
          <div v-else-if="isPdfFile(currentFile)" class="pdf-preview">
            <div class="pdf-container">
              <iframe 
                :src="getPdfViewerUrl(currentFile)"
                class="pdf-iframe"
                frameborder="0"
              ></iframe>
            </div>
            
            <!-- PDF 工具栏 -->
            <div class="pdf-toolbar">
              <el-button @click="openInNewTab(currentFile)" type="primary" size="small">
                <i class="fas fa-external-link-alt"></i>
                在新标签页打开
              </el-button>
            </div>
          </div>
          
          <!-- 文本文件预览 -->
          <div v-else-if="isTextFile(currentFile)" class="text-preview">
            <div class="text-container">
              <pre class="text-content">{{ textContent }}</pre>
            </div>
            
            <!-- 文本工具栏 -->
            <div class="text-toolbar">
              <el-button @click="copyTextContent" type="primary" size="small">
                <i class="fas fa-copy"></i>
                复制内容
              </el-button>
            </div>
          </div>
          
          <!-- 视频预览 -->
          <div v-else-if="isVideoFile(currentFile)" class="video-preview">
            <video 
              :src="currentFile.url || currentFile.preview"
              controls
              class="preview-video"
              @loadedmetadata="onVideoLoad"
            >
              您的浏览器不支持视频播放。
            </video>
          </div>
          
          <!-- 音频预览 -->
          <div v-else-if="isAudioFile(currentFile)" class="audio-preview">
            <div class="audio-container">
              <div class="audio-info">
                <i class="fas fa-music"></i>
                <h4>{{ currentFile.name }}</h4>
              </div>
              <audio 
                :src="currentFile.url || currentFile.preview"
                controls
                class="preview-audio"
              >
                您的浏览器不支持音频播放。
              </audio>
            </div>
          </div>
          
          <!-- 不支持预览的文件 -->
          <div v-else class="unsupported-preview">
            <div class="unsupported-content">
              <div class="unsupported-icon">
                <i :class="getFileIcon(currentFile)"></i>
              </div>
              <h3>无法预览此文件</h3>
              <p>{{ currentFile.name }}</p>
              <p class="file-type">文件类型：{{ currentFile.type || '未知' }}</p>
              
              <div class="unsupported-actions">
                <el-button 
                  @click="downloadFile(currentFile)"
                  type="primary"
                  size="large"
                >
                  <i class="fas fa-download"></i>
                  下载文件
                </el-button>
              </div>
            </div>
          </div>
        </div>
        
        <!-- 预览侧边栏 -->
        <div class="preview-sidebar">
          <div class="file-details">
            <h4>文件详情</h4>
            
            <div class="details-grid">
              <div class="detail-item">
                <label>文件名：</label>
                <span>{{ currentFile.name }}</span>
              </div>
              <div class="detail-item">
                <label>文件大小：</label>
                <span>{{ formatFileSize(currentFile.size) }}</span>
              </div>
              <div class="detail-item">
                <label>文件类型：</label>
                <span>{{ currentFile.type || '未知' }}</span>
              </div>
              <div class="detail-item" v-if="currentFile.lastModified">
                <label>修改时间：</label>
                <span>{{ formatDateTime(currentFile.lastModified) }}</span>
              </div>
              <div class="detail-item" v-if="imageInfo.width">
                <label>图片尺寸：</label>
                <span>{{ imageInfo.width }}×{{ imageInfo.height }}</span>
              </div>
              <div class="detail-item" v-if="videoInfo.duration">
                <label>视频时长：</label>
                <span>{{ formatDuration(videoInfo.duration) }}</span>
              </div>
            </div>
          </div>
          
          <!-- 文件导航 -->
          <div class="file-navigation" v-if="files.length > 1">
            <h4>文件导航</h4>
            
            <div class="nav-controls">
              <el-button 
                @click="previousFile"
                :disabled="selectedFileIndex === 0"
                size="small"
                class="nav-btn"
              >
                <i class="fas fa-chevron-left"></i>
                上一个
              </el-button>
              <span class="nav-info">
                {{ selectedFileIndex + 1 }} / {{ files.length }}
              </span>
              <el-button 
                @click="nextFile"
                :disabled="selectedFileIndex === files.length - 1"
                size="small"
                class="nav-btn"
              >
                下一个
                <i class="fas fa-chevron-right"></i>
              </el-button>
            </div>
            
            <!-- 文件缩略图列表 -->
            <div class="nav-thumbnails">
              <div 
                v-for="(file, index) in files" 
                :key="file.id || index"
                class="nav-thumbnail"
                :class="{ active: index === selectedFileIndex }"
                @click="selectFile(index)"
              >
                <div class="nav-thumb-icon">
                  <img 
                    v-if="file.type && file.type.startsWith('image/')"
                    :src="file.url || file.preview"
                    :alt="file.name"
                  />
                  <i v-else :class="getFileIcon(file)"></i>
                </div>
                <span class="nav-thumb-name">{{ file.name }}</span>
              </div>
            </div>
          </div>
          
          <!-- 操作按钮 -->
          <div class="preview-actions">
            <el-button 
              @click="downloadFile(currentFile)"
              type="primary"
              class="action-btn"
            >
              <i class="fas fa-download"></i>
              下载文件
            </el-button>
            <el-button 
              @click="shareFile(currentFile)"
              type="success"
              class="action-btn"
              v-if="allowShare"
            >
              <i class="fas fa-share-alt"></i>
              分享文件
            </el-button>
            <el-button 
              @click="removeCurrentFile"
              type="danger"
              class="action-btn"
              v-if="allowRemove"
            >
              <i class="fas fa-trash"></i>
              删除文件
            </el-button>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, computed, watch, nextTick } from 'vue'
import { ElButton, ElButtonGroup, ElDialog, ElMessage, ElMessageBox } from 'element-plus'

// Props
const props = defineProps({
  files: {
    type: Array,
    default: () => []
  },
  allowRemove: {
    type: Boolean,
    default: true
  },
  allowShare: {
    type: Boolean,
    default: false
  },
  viewMode: {
    type: String,
    default: 'grid' // 'grid' | 'list'
  }
})

// Emits
const emit = defineEmits(['file-select', 'file-remove', 'file-download', 'file-share'])

// 响应式数据
const viewMode = ref(props.viewMode)
const selectedFileIndex = ref(0)
const previewDialogVisible = ref(false)
const currentFile = ref(null)
const textContent = ref('')
const imageInfo = ref({ width: 0, height: 0 })
const videoInfo = ref({ duration: 0 })

// 图片预览相关
const imageZoom = ref(1)
const imageRotation = ref(0)

// 计算属性
const currentFileIndex = computed(() => selectedFileIndex.value)

// 方法
const toggleViewMode = () => {
  viewMode.value = viewMode.value === 'grid' ? 'list' : 'grid'
}

const selectFile = (index) => {
  selectedFileIndex.value = index
  emit('file-select', props.files[index], index)
}

const previewFile = async (file, index) => {
  currentFile.value = file
  selectedFileIndex.value = index
  previewDialogVisible.value = true
  
  // 重置预览状态
  imageZoom.value = 1
  imageRotation.value = 0
  textContent.value = ''
  imageInfo.value = { width: 0, height: 0 }
  videoInfo.value = { duration: 0 }
  
  // 加载文件内容
  if (isTextFile(file)) {
    await loadTextContent(file)
  }
}

const closePreview = () => {
  previewDialogVisible.value = false
  currentFile.value = null
}

const removeFile = async (index) => {
  try {
    await ElMessageBox.confirm(
      '确定要删除这个文件吗？',
      '确认删除',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )
    
    emit('file-remove', props.files[index], index)
    ElMessage.success('文件已删除')
  } catch {
    // 用户取消
  }
}

const removeCurrentFile = async () => {
  await removeFile(selectedFileIndex.value)
  
  // 如果删除的是当前预览的文件，关闭预览
  if (props.files.length === 0) {
    closePreview()
  } else {
    // 调整索引
    if (selectedFileIndex.value >= props.files.length) {
      selectedFileIndex.value = props.files.length - 1
    }
    currentFile.value = props.files[selectedFileIndex.value]
  }
}

const downloadFile = (file) => {
  if (file.url) {
    const link = document.createElement('a')
    link.href = file.url
    link.download = file.name
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
  } else if (file.file) {
    // 如果是 File 对象，创建临时 URL
    const url = URL.createObjectURL(file.file)
    const link = document.createElement('a')
    link.href = url
    link.download = file.name
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
    URL.revokeObjectURL(url)
  }
  
  emit('file-download', file)
  ElMessage.success('文件下载已开始')
}

const downloadAll = () => {
  props.files.forEach(file => {
    downloadFile(file)
  })
}

const shareFile = (file) => {
  emit('file-share', file)
  ElMessage.success('分享链接已生成')
}

const previousFile = () => {
  if (selectedFileIndex.value > 0) {
    const newIndex = selectedFileIndex.value - 1
    selectFile(newIndex)
    previewFile(props.files[newIndex], newIndex)
  }
}

const nextFile = () => {
  if (selectedFileIndex.value < props.files.length - 1) {
    const newIndex = selectedFileIndex.value + 1
    selectFile(newIndex)
    previewFile(props.files[newIndex], newIndex)
  }
}

// 文件类型判断
const isImageFile = (file) => {
  return file.type && file.type.startsWith('image/')
}

const isPdfFile = (file) => {
  return file.type === 'application/pdf' || file.name.toLowerCase().endsWith('.pdf')
}

const isTextFile = (file) => {
  const textTypes = [
    'text/plain',
    'text/html',
    'text/css',
    'text/javascript',
    'application/json',
    'application/xml'
  ]
  const textExtensions = ['.txt', '.md', '.json', '.xml', '.html', '.css', '.js', '.ts']
  
  return textTypes.includes(file.type) || 
         textExtensions.some(ext => file.name.toLowerCase().endsWith(ext))
}

const isVideoFile = (file) => {
  return file.type && file.type.startsWith('video/')
}

const isAudioFile = (file) => {
  return file.type && file.type.startsWith('audio/')
}

const canPreview = (file) => {
  return isImageFile(file) || isPdfFile(file) || isTextFile(file) || 
         isVideoFile(file) || isAudioFile(file)
}

// 文件图标
const getFileIcon = (file) => {
  if (isImageFile(file)) return 'fas fa-image'
  if (isPdfFile(file)) return 'fas fa-file-pdf'
  if (isTextFile(file)) return 'fas fa-file-alt'
  if (isVideoFile(file)) return 'fas fa-file-video'
  if (isAudioFile(file)) return 'fas fa-file-audio'
  if (file.type && file.type.includes('zip')) return 'fas fa-file-archive'
  if (file.type && file.type.includes('word')) return 'fas fa-file-word'
  if (file.type && file.type.includes('excel')) return 'fas fa-file-excel'
  if (file.type && file.type.includes('powerpoint')) return 'fas fa-file-powerpoint'
  
  return 'fas fa-file'
}

const getFileExtension = (filename) => {
  const ext = filename.split('.').pop()
  return ext ? ext.toUpperCase() : 'FILE'
}

// PDF 预览
const getPdfViewerUrl = (file) => {
  const fileUrl = file.url || URL.createObjectURL(file.file)
  return `https://mozilla.github.io/pdf.js/web/viewer.html?file=${encodeURIComponent(fileUrl)}`
}

const openInNewTab = (file) => {
  const fileUrl = file.url || URL.createObjectURL(file.file)
  window.open(fileUrl, '_blank')
}

// 文本文件加载
const loadTextContent = async (file) => {
  try {
    let text = ''
    
    if (file.file) {
      text = await file.file.text()
    } else if (file.url) {
      const response = await fetch(file.url)
      text = await response.text()
    }
    
    textContent.value = text
  } catch (error) {
    console.error('Failed to load text content:', error)
    textContent.value = '无法加载文件内容'
  }
}

const copyTextContent = async () => {
  try {
    await navigator.clipboard.writeText(textContent.value)
    ElMessage.success('内容已复制到剪贴板')
  } catch (error) {
    ElMessage.error('复制失败')
  }
}

// 图片预览控制
const zoomIn = () => {
  imageZoom.value = Math.min(imageZoom.value * 1.2, 5)
}

const zoomOut = () => {
  imageZoom.value = Math.max(imageZoom.value / 1.2, 0.1)
}

const resetZoom = () => {
  imageZoom.value = 1
  imageRotation.value = 0
}

const rotateImage = () => {
  imageRotation.value = (imageRotation.value + 90) % 360
}

// 事件处理
const onImageLoad = (event) => {
  const img = event.target
  imageInfo.value = {
    width: img.naturalWidth,
    height: img.naturalHeight
  }
}

const onImageError = () => {
  ElMessage.error('图片加载失败')
}

const onVideoLoad = (event) => {
  const video = event.target
  videoInfo.value = {
    duration: video.duration
  }
}

const handleImageError = () => {
  // 图片加载失败的处理
}

// 工具函数
const formatFileSize = (bytes) => {
  if (bytes === 0) return '0 B'
  
  const k = 1024
  const sizes = ['B', 'KB', 'MB', 'GB']
  const i = Math.floor(Math.log(bytes) / Math.log(k))
  
  return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
}

const formatDate = (date) => {
  if (typeof date === 'number') {
    date = new Date(date)
  }
  
  return date.toLocaleDateString('zh-CN')
}

const formatDateTime = (date) => {
  if (typeof date === 'number') {
    date = new Date(date)
  }
  
  return date.toLocaleString('zh-CN')
}

const formatDuration = (seconds) => {
  const hours = Math.floor(seconds / 3600)
  const minutes = Math.floor((seconds % 3600) / 60)
  const secs = Math.floor(seconds % 60)
  
  if (hours > 0) {
    return `${hours}:${minutes.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`
  }
  
  return `${minutes}:${secs.toString().padStart(2, '0')}`
}

// 监听文件变化
watch(() => props.files, (newFiles) => {
  if (newFiles.length === 0) {
    selectedFileIndex.value = 0
    closePreview()
  } else if (selectedFileIndex.value >= newFiles.length) {
    selectedFileIndex.value = newFiles.length - 1
  }
}, { immediate: true })
</script>

<style scoped>
.file-preview-component {
  width: 100%;
}

.file-list {
  background: white;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.list-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 1px solid #f0f0f0;
}

.list-header h4 {
  color: #2c5aa0;
  font-size: 16px;
  font-weight: 600;
  margin: 0;
  display: flex;
  align-items: center;
  gap: 8px;
}

.list-actions {
  display: flex;
  gap: 10px;
}

.file-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 15px;
}

.file-grid.list-mode {
  grid-template-columns: 1fr;
  gap: 8px;
}

.file-item {
  background: #f8f9fa;
  border: 1px solid #e9ecef;
  border-radius: 8px;
  padding: 12px;
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  gap: 12px;
}

.file-grid:not(.list-mode) .file-item {
  flex-direction: column;
  text-align: center;
}

.file-item:hover {
  border-color: #2c5aa0;
  background: #f0f7ff;
  transform: translateY(-1px);
}

.file-item.active {
  border-color: #2c5aa0;
  background: #e6f7ff;
}

.file-thumbnail {
  position: relative;
  width: 60px;
  height: 60px;
  flex-shrink: 0;
}

.file-grid:not(.list-mode) .file-thumbnail {
  width: 80px;
  height: 80px;
}

.thumbnail-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 6px;
}

.file-icon {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #2c5aa0;
  color: white;
  border-radius: 6px;
  font-size: 24px;
}

.file-type-badge {
  position: absolute;
  bottom: -4px;
  right: -4px;
  background: #2c5aa0;
  color: white;
  font-size: 10px;
  padding: 2px 4px;
  border-radius: 3px;
  font-weight: 600;
}

.file-info {
  flex: 1;
  min-width: 0;
}

.file-grid:not(.list-mode) .file-info {
  width: 100%;
  margin-top: 8px;
}

.file-name {
  color: #333;
  font-size: 14px;
  font-weight: 500;
  margin-bottom: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.file-meta {
  display: flex;
  gap: 10px;
  color: #999;
  font-size: 12px;
}

.file-grid:not(.list-mode) .file-meta {
  justify-content: center;
}

.file-actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.file-grid:not(.list-mode) .file-actions {
  margin-top: 8px;
}

.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: #999;
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 20px;
}

.empty-text h3 {
  color: #666;
  font-size: 18px;
  font-weight: 500;
  margin: 0 0 8px 0;
}

.empty-text p {
  color: #999;
  font-size: 14px;
  margin: 0;
}

/* 预览对话框样式 */
.file-preview-dialog {
  --el-dialog-padding-primary: 0;
}

.preview-container {
  display: flex;
  height: 80vh;
  min-height: 600px;
}

.preview-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: #f5f5f5;
  position: relative;
}

.image-preview {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.image-container {
  position: relative;
  max-width: 100%;
  max-height: 100%;
}

.preview-image {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  transform: scale(var(--zoom, 1)) rotate(var(--rotation, 0deg));
  transition: transform 0.3s ease;
}

.image-toolbar {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0, 0, 0, 0.7);
  border-radius: 6px;
  padding: 8px;
}

.pdf-preview {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.pdf-container {
  flex: 1;
}

.pdf-iframe {
  width: 100%;
  height: 100%;
  border: none;
}

.pdf-toolbar {
  padding: 15px;
  background: white;
  border-top: 1px solid #f0f0f0;
  text-align: center;
}

.text-preview {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.text-container {
  flex: 1;
  padding: 20px;
  background: white;
  overflow: auto;
}

.text-content {
  font-family: 'Courier New', monospace;
  font-size: 14px;
  line-height: 1.5;
  color: #333;
  white-space: pre-wrap;
  word-wrap: break-word;
  margin: 0;
}

.text-toolbar {
  padding: 15px;
  background: white;
  border-top: 1px solid #f0f0f0;
  text-align: center;
}

.video-preview {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.preview-video {
  max-width: 100%;
  max-height: 100%;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.audio-preview {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}

.audio-container {
  text-align: center;
  background: white;
  padding: 40px;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.audio-info {
  margin-bottom: 20px;
}

.audio-info i {
  font-size: 48px;
  color: #2c5aa0;
  margin-bottom: 15px;
}

.audio-info h4 {
  color: #333;
  font-size: 16px;
  font-weight: 600;
  margin: 0;
}

.preview-audio {
  width: 100%;
  max-width: 400px;
}

.unsupported-preview {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}

.unsupported-content {
  text-align: center;
  max-width: 400px;
}

.unsupported-icon {
  font-size: 64px;
  color: #999;
  margin-bottom: 20px;
}

.unsupported-content h3 {
  color: #666;
  font-size: 18px;
  font-weight: 500;
  margin: 0 0 10px 0;
}

.unsupported-content p {
  color: #999;
  font-size: 14px;
  margin: 5px 0;
}

.file-type {
  font-style: italic;
}

.unsupported-actions {
  margin-top: 30px;
}

.preview-sidebar {
  width: 320px;
  background: white;
  border-left: 1px solid #f0f0f0;
  display: flex;
  flex-direction: column;
}

.file-details {
  padding: 20px;
  border-bottom: 1px solid #f0f0f0;
}

.file-details h4 {
  color: #333;
  font-size: 16px;
  font-weight: 600;
  margin: 0 0 15px 0;
}

.details-grid {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.detail-item {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 10px;
}

.detail-item label {
  color: #666;
  font-size: 13px;
  font-weight: 500;
  flex-shrink: 0;
}

.detail-item span {
  color: #333;
  font-size: 13px;
  text-align: right;
  word-break: break-all;
}

.file-navigation {
  flex: 1;
  padding: 20px;
  border-bottom: 1px solid #f0f0f0;
  overflow-y: auto;
}

.file-navigation h4 {
  color: #333;
  font-size: 16px;
  font-weight: 600;
  margin: 0 0 15px 0;
}

.nav-controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 15px;
}

.nav-btn {
  flex: 1;
  max-width: 80px;
}

.nav-info {
  color: #666;
  font-size: 14px;
  font-weight: 500;
}

.nav-thumbnails {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 300px;
  overflow-y: auto;
}

.nav-thumbnail {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px;
  border-radius: 6px;
  cursor: pointer;
  transition: background-color 0.2s ease;
}

.nav-thumbnail:hover {
  background: #f0f7ff;
}

.nav-thumbnail.active {
  background: #e6f7ff;
  border: 1px solid #2c5aa0;
}

.nav-thumb-icon {
  width: 32px;
  height: 32px;
  flex-shrink: 0;
  border-radius: 4px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f0f0f0;
}

.nav-thumb-icon img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.nav-thumb-icon i {
  color: #666;
  font-size: 14px;
}

.nav-thumb-name {
  color: #333;
  font-size: 12px;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.preview-actions {
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.action-btn {
  width: 100%;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .file-grid {
    grid-template-columns: 1fr;
  }
  
  .list-header {
    flex-direction: column;
    gap: 15px;
    align-items: stretch;
  }
  
  .list-actions {
    justify-content: center;
  }
  
  .preview-container {
    flex-direction: column;
    height: auto;
  }
  
  .preview-sidebar {
    width: 100%;
    border-left: none;
    border-top: 1px solid #f0f0f0;
  }
  
  .file-item {
    flex-direction: row;
    text-align: left;
  }
  
  .file-thumbnail {
    width: 60px;
    height: 60px;
  }
  
  .file-info {
    margin-top: 0;
  }
  
  .file-meta {
    justify-content: flex-start;
  }
  
  .file-actions {
    margin-top: 0;
  }
}
</style>